- Single Line Comment
- Multi Line Comment
Variables
Varibles are a containers which can store a certain type of data. Let's take an example of your water bottle.
You have an water bottle which can store a liquid water, can you determine what roles are the water bottle, liquid and water is playing? In this example the water bottle is playing the role of a variable as it will store the water which is playing the role of data that will be stored, so at last we can determine that the liquid is a type of the data that is being stored in the water bottle.
Way to declare a variable:
int roll_no = 12;
In here int is a datatype, roll_no is the variable and 12 is the value to be stored in the variable roll_no. After it is executed a space is allocated in the ram to store the variable.
Rules for naming a variable:
- A variable name in C# can only contain letters, numbers or underscores(_).
- Variable name can’t start with a number although it can contain number in between the variable or at the end. Example: name & first_name is valid but 3name is invalid naming convention.
- We cannot use a reserved keyword as an variable name because they already have their own meanings and purposes to full fill. Example: if, Console, while, etc.
- Variable names are case sensitive, like firstName is not the same as Firstname.
Data Types
Datatypes specifies the types of a variable.
int: it stands for integer and it has a storage range of -2,147,483,648 to 2,147,483,647.
int num = 12;
long: it refers to integer and it has a storage range of -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
long num = 12l;
byte: byte also refers to integral numbers, but it has a very low range of 0 to 255.
byte num = 12;
float: float refers to floating point numbers, which have decimal places in the numbers, such as 1.34, 5.64.
Float has a range of ±1.5 x 10−45 to ±3.4 x 1038
It uses 8 bytes of storage and has a precision of 7 digits, it means that if you store a number 1.23456789, the number will be rounded off to 1.2345678.
float num = 12f;
decimal: decimal stores a decimal number but has a smaller range than float and
double. However, it has a much greater precision of approximately 28-29
digits.
If your program requires a high degree of precision you should use a decimal data type.
It has a range of ±1.0 x 10-28 to ±7.9228 x 1028
decimal num5 = 12324.33M;
char: char stands for character and is used to store single characters such
as ‘A’, 'B', 'c', 'd' etc.
char ch = 'A';
bool: bool stands for boolean and can only hold two values: true and false.
bool isRaining = true;
bool isWalking = false;
string: It stores texts in a variable, in a string the text is enclosed in double quotes.
string name = "Priyanshu Bhattacharjee"
Concatenation
To join 2 or more strings is known as Concatenation, we use (+) sign to concatinate strings. Example:
using System;
class HelloWorld {
static void Main() {
string concat = "Hello, " + "Priyanshu";
Console.WriteLine(concat);
}
}
String Methods
Length
It tells that how long the string is.
string concat = "Priyanshu";
Console.WriteLine(concat.Length);
Equals
We can use the Equals() method to compare 2 strings.
string first = "Priyanshu";
string second = "Priyanshu";
Console.WriteLine(first.Equals(second));
Before getting to the IndexOf() and ElementAt() methods we need to understand about indexing.
Indexing
Indexing helps us determine where a partcular character in the string is present. Always indexing in programming starts from 0. Example: There's a string ABC and we want the index value of B, so the index of B would be 1 as A is 0 and C is 2.
IndexOf() & ElementAt()
IndexOf() gives index of a particular character availiable in the string. Example: ABC is a string and the index of B is 1.
ElementAt() gives the values present at a certain index in the availiable string. ABC is a string and we want the value present at index 2 and we get C as it's present in the index 2.
using System;
using System.Linq;
namespace HelloWorld
{
public class Program
{
public static void Main(string[] args)
{
string first = "Priyanshu";
Console.WriteLine(first.IndexOf("P"));
Console.WriteLine(first.ElementAt(0));
}
}
}
Here, you noticed that we are including another namespace named System.Linq, Linq is a part of System namespace and it contains the ElementAt() method.
3.2 Comments, Variables & Data Types
Comments
Comments are specific lines that are ignored by the compiler, Comments helps to make our code more readable for other programmers.
There are 2 types of comments:
Variables
Varibles are a containers which can store a certain type of data. Let's take an example of your water bottle.
You have an water bottle which can store a liquid water, can you determine what roles are the water bottle, liquid and water is playing? In this example the water bottle is playing the role of a variable as it will store the water which is playing the role of data that will be stored, so at last we can determine that the liquid is a type of the data that is being stored in the water bottle.
Way to declare a variable:
int roll_no = 12;
In here int is a datatype, roll_no is the variable and 12 is the value to be stored in the variable roll_no. After it is executed a space is allocated in the ram to store the variable.
Rules for naming a variable:
Data Types
Datatypes specifies the types of a variable.
int: it stands for integer and it has a storage range of -2,147,483,648 to 2,147,483,647.
int num = 12;
long: it refers to integer and it has a storage range of -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
long num = 12l;
byte: byte also refers to integral numbers, but it has a very low range of 0 to 255.
byte num = 12;
float: float refers to floating point numbers, which have decimal places in the numbers, such as 1.34, 5.64.
Float has a range of ±1.5 x 10−45 to ±3.4 x 1038
It uses 8 bytes of storage and has a precision of 7 digits, it means that if you store a number 1.23456789, the number will be rounded off to 1.2345678.
float num = 12f;
decimal: decimal stores a decimal number but has a smaller range than float and double. However, it has a much greater precision of approximately 28-29 digits.
If your program requires a high degree of precision you should use a decimal data type.
It has a range of ±1.0 x 10-28 to ±7.9228 x 1028
decimal num5 = 12324.33M;
char: char stands for character and is used to store single characters such as ‘A’, 'B', 'c', 'd' etc.
char ch = 'A';
bool: bool stands for boolean and can only hold two values: true and false.
bool isRaining = true; bool isWalking = false;
string: It stores texts in a variable, in a string the text is enclosed in double quotes.
string name = "Priyanshu Bhattacharjee"
Concatenation
To join 2 or more strings is known as Concatenation, we use (+) sign to concatinate strings. Example:
String Methods
Length
It tells that how long the string is.
string concat = "Priyanshu"; Console.WriteLine(concat.Length);
Equals
We can use the Equals() method to compare 2 strings.
string first = "Priyanshu"; string second = "Priyanshu"; Console.WriteLine(first.Equals(second));
Before getting to the IndexOf() and ElementAt() methods we need to understand about indexing.
Indexing
Indexing helps us determine where a partcular character in the string is present. Always indexing in programming starts from 0. Example: There's a string ABC and we want the index value of B, so the index of B would be 1 as A is 0 and C is 2.
IndexOf() & ElementAt()
IndexOf() gives index of a particular character availiable in the string. Example: ABC is a string and the index of B is 1.
ElementAt() gives the values present at a certain index in the availiable string. ABC is a string and we want the value present at index 2 and we get C as it's present in the index 2.
Here, you noticed that we are including another namespace named System.Linq, Linq is a part of System namespace and it contains the ElementAt() method.